package com.smartpower.wattmeter.ui;

import android.Manifest;
import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.android.basiclib.base.BaseFragment;
import com.android.basiclib.uitls.NetWorkUtil;
import com.android.basiclib.uitls.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.smartpower.wattmeter.R;
import com.smartpower.wattmeter.adapter.OrderAdapter;
import com.yanzhenjie.permission.Action;
import com.yanzhenjie.permission.AndPermission;

import java.util.ArrayList;
import java.util.List;

public class HomeOrderFragment extends BaseFragment {

    private SwipeRefreshLayout mRefreshLayout;
    private RecyclerView mRecyclerView;
    private OrderAdapter mOrderAdapter;

    @Override
    protected int getLayoutResId() {
        return R.layout.fragment_home_order;
    }

    @Override
    protected void init() {
        initRV();
        initListener();
    }

    private void initListener() {
        //item点击
        mOrderAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {

                AndPermission.with(mActivity).runtime()
                        .permission(Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.WRITE_EXTERNAL_STORAGE)
                        .onGranted(data -> {
                            OrderDetailActivity.startInstance();
                        })
                        .onDenied(data -> {
                            ToastUtils.makeText(mActivity, "缺少定位权限，无法进入地图展示");
                        })
                        .start();

            }
        });

        //刷新操作
        mRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mRefreshLayout.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mRefreshLayout.setRefreshing(false);
                    }
                }, 2000);
            }
        });
    }

    private void initRV() {
        List<String> datas = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            datas.add("1");
        }
        mOrderAdapter = new OrderAdapter(datas);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(mActivity));
        mOrderAdapter.bindToRecyclerView(mRecyclerView);
    }

    @Override
    public void onNetworkConnectionChanged(boolean isConnected, NetWorkUtil.NetworkType networkType) {
    }

    @Override
    protected void dataObserver() {
    }

    @Override
    protected void initViews(View view) {
        mRefreshLayout = view.findViewById(R.id.refresh_layout);
        mRecyclerView = view.findViewById(R.id.recycler_view);
    }

}
